/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Procedures;

import entities.Loan;

/**
 *
 * @author SIX-14
 */
public class LoanTable {
    private Loan[] table;

    public LoanTable(){
        this.table= new Loan[0];

    }

    public Loan[] getTable() {
        return table;
    }

    public void setTable(Loan[] table) {
        this.table = table;
    }



    public void addLoan(int id, String type_user, String coputerRoom, String ipComputer, int numberComputer){
        Loan[] loan2=new Loan[getTable().length+1];
        for(int i=0; i<getTable().length; i++){
            loan2[i]=getTable()[i];
        }
        loan2[getTable().length]=new Loan(id, type_user, coputerRoom, ipComputer, numberComputer);
        setTable(loan2);
    }

    public Loan removeLoan(int id, String type_user, String computerRoom){
        Loan out=new Loan();
        int token=0;
        for(int i=0; i<getTable().length; i++){
                if(getTable()[i].getId()==id && getTable()[i].getType_user().equals(type_user) && getTable()[i].getComputerRoom().equals(computerRoom)){
                    token=1;
                }
            }
        if(token==1){
            token=0;
            Loan[] loan2=new Loan[getTable().length-1];
            for(int i=0; i<getTable().length; i++){
                if(token==0){
                    if(getTable()[i].getId()==id && getTable()[i].getType_user().equals(type_user) && getTable()[i].getComputerRoom().equals(computerRoom)){
                        token=1;
                    }
                }else{
                    token=2;
                }
                if(token==0){
                    loan2[i]=getTable()[i];
                }else if(token==1){
                    out=getTable()[i];
                }else{
                    loan2[i-1]=getTable()[i];
                }
            }
            setTable(loan2);
        }
        return out;
    }

    public boolean find_user(int id, String type_user){
        for(int i=0; i<getTable().length;i++){
            if(getTable()[i].getId()==id){
                if(getTable()[i].getType_user().equals(type_user)){
                    return true;
                }
            }
        }
        return false;

    }

    public String findNumber(int id, String type_user){
        String number="";
        for(int i=0; i<getTable().length;i++){
            if(getTable()[i].getId()==id){
                if(getTable()[i].getType_user().equals(type_user)){
                    number= String.valueOf(getTable()[i].getNumberComputer());
                }
            }
        }
        return number;
    }

    public static void main(String[] args) {
        LoanTable lt = new LoanTable();

    }

}
